﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.Common;
using YY.UI.BaseDataSvc;

namespace YY.UI
{
    public partial class VerifyFee : BaseFrm
    {
        /// <summary>
        /// 用户源数据临时变量
        /// </summary>
        IEnumerable<TB_User> users = null;

        /// <summary>
        /// 通用分类数据临时变量
        /// </summary>
        IEnumerable<TB_Options> options = null;

        /// <summary>
        /// 客户源数据临时变量
        /// </summary>
        IEnumerable<TB_Clients> clients = null;
        
        public VerifyFee()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
        }

        private void VerifyFee_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            this.users = this.BaseDataProxy.FindUsers(null, null);
            this.clients = this.BaseDataProxy.FindClients(null, null, null, null, null);
            this.options = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.费目类型.ToString(), null, null);
            initDgvColumns();
            init();
            search();
        }

        /// <summary>
        /// 控件数据初始化
        /// </summary>
        private void init()
        {
            this.dtpEDate.Value = DateTime.Now;
            this.dtpSDate.Value = DateTime.Now.AddDays(-7);
            
            Common.BindEnumToComboBox(this.cbBoxOrderStatus, typeof(EnumCommon.OrderStatus), null);
            Common.BindEnumToComboBox(this.cbBoxFeeType, typeof(EnumCommon.FeeType), null);
            Common.BindOptionsComboBox(this.cbBoxItem, this.options, EnumCommon.OptionType.费目类型);

            Common.ComboBoxBindDatas<TB_User>(this.cbBoxOP, this.users, "userName", "id");
            this.cbBoxOP.KeyPress += new KeyPressEventHandler(cbBoxUser_KeyPress);
            Common.ComboBoxBindDatas<TB_User>(this.cbBoxBizMan, this.users, "userName", "id");
            this.cbBoxBizMan.KeyPress += new KeyPressEventHandler(cbBoxUser_KeyPress);
        }

        /// <summary>
        /// 初始化表格列
        /// </summary>
        private void initDgvColumns()
        {

            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;//AutoSize十分影响性能
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "费用明细id", false, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderId", "作业单id", false, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "作业单号", true, true, null);
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
            Common.BindEnumToDataGridViewComboBoxColumn(cbc, typeof(EnumCommon.OrderStatus));
            Common.AddDgvColumn(this.dataGridView1, cbc, "OrderStatus", "流程状态", true, false, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "BizMan", "业务员", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "FeeType", "费用类型", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewCheckBoxColumn(), "IsVerify", "审批", true, false, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Item", "费目名称", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "FeeCode", "账单号", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewCheckBoxColumn(), "Operator", "制单人", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewCalendarColumn(), "OPTime", "制单时间", true, true, null);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            search();
        }

        /// <summary>
        /// 数据搜索
        /// </summary>
        private void search()
        {
            int? op = null;
            if (this.cbBoxOP.SelectedItem != null)
                op = (this.cbBoxOP.SelectedItem as TB_User).id;

            int? bizMan = null;
            if (this.cbBoxBizMan.SelectedItem != null)
                bizMan = (this.cbBoxBizMan.SelectedItem as TB_User).id;

            bool? isVerify = null;
            if (!string.IsNullOrEmpty(this.cbBoxIsVerify.Text))
                isVerify = this.cbBoxIsVerify.Text == "是";

            int? feeType = null;
            if (this.cbBoxFeeType.SelectedItem != null)
                feeType = (int)Enum.Parse(typeof(EnumCommon.FeeType), this.cbBoxFeeType.Text);

            int? item = null;
            if(this.cbBoxItem.SelectedItem!=null)
                item = (this.cbBoxItem.SelectedItem as TB_Options).id;

            DateTime dtStart = this.dtpSDate.Value.Date;
            DateTime dtEnd = this.dtpEDate.Value.AddDays(1).Date.AddSeconds(-1);

            IEnumerable<QFee> datas = this.BaseDataProxy.FindFees(this.txtBoxOrderCode.Text, this.cbBoxOrderStatus.Text, bizMan, op, dtStart, dtEnd, feeType, this.txtBoxFeeCode.Text, item, null, isVerify);
            DataTable dt = OTConverter.ConvertToDataTable<QFee>(this.dataGridView1);
            this.dataGridView1.DataSource = dt;
        }

        /// <summary>
        /// 用户选择框回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbBoxUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ComboBox cbBox = sender as ComboBox;
                if (this.users != null)
                {
                    TB_User[] users = this.users.Where(p => p.userName.Contains(cbBox.Text) || p.code.Contains(cbBox.Text)).ToArray();
                    Common.ComboBoxMatchUser(cbBox, users, this);
                }
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int orderId = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[1].Value);
            EditOrderFrm frm = new EditOrderFrm();
            frm.EditMode = true;
            frm.OrderId = orderId;
            frm.ShowDialog(this);
        }
    }
}
